#region File Header
//-----------------------------------------------------------------------------
// SibLib
//
// Copyright (C) 2010 Julien Villers
// This program is distributed under the terms of the 
// GNU Lesser General Public License (LGPL).
// See Docs/lgpl.txt and Docs/gpl.txt
//
// This file is based on GameStateManagement sample (C) Microsoft.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace SibLib.GameState
{
    /// <summary>
    /// Base class represents of a single entry in a MenuScreen. 
    /// This version just draws the entry text string, 
    /// and provides an event that will be raised when the menu entry is selected.
    /// </summary>
    /// <remarks>
    /// 'Selected' is not a very appropriate event name. It occurs when the entry is picked when the player validates.
    /// </remarks>
    public class MenuEntry
    {
        #region Fields

        /// <summary>
        /// The text rendered for this entry.
        /// </summary>
        protected string m_Text;

        /// <summary>
        /// Tracks a fading selection effect on the entry.
        /// </summary>
        /// <remarks>
        /// The entries transition out of the selection effect when they are deselected.
        /// </remarks>
        protected float m_SelectionFade;

        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the text of this menu entry.
        /// </summary>
        public string Text
        {
            get { return m_Text; }
            set { m_Text = value; }
        }
        #endregion

        #region Events

        /// <summary>
        /// Event raised when the menu entry is selected.
        /// </summary>
        public event EventHandler<PlayerIndexEventArgs> Selected;

        /// <summary>
        /// Method for raising the Selected event.
        /// </summary>
        protected internal virtual void OnSelectEntry(PlayerIndex playerIndex)
        {
            if (Selected != null)
                Selected(this, new PlayerIndexEventArgs(playerIndex));
        }

        public event EventHandler<InputStateEventArgs> CustomInput;

        protected internal virtual void OnCustomInput(InputState inputState, PlayerIndex playerIndex)
        {
            if (CustomInput != null)
                CustomInput(this, new InputStateEventArgs(inputState, playerIndex));
        }

        #endregion

        #region Initialization

        /// <summary>
        /// Constructs a new menu entry with the specified text.
        /// </summary>
        public MenuEntry(string text)
        {
            this.m_Text = text;
        }

        #endregion

        #region Update and Draw

        /// <summary>
        /// Updates the menu entry.
        /// </summary>
        public virtual void Update(MenuScreen screen, bool isSelected, GameTime gameTime)
        {
            // When the menu selection changes, entries gradually fade between
            // their selected and deselected appearance, rather than instantly
            // popping to the new state.
            float fadeSpeed = (float)gameTime.ElapsedGameTime.TotalSeconds * 4;

            if (isSelected)
                m_SelectionFade = Math.Min(m_SelectionFade + fadeSpeed, 1);
            else
                m_SelectionFade = Math.Max(m_SelectionFade - fadeSpeed, 0);
        }

        /// <summary>
        /// Compute color and scale, based on selection and time.
        /// </summary>
        /// <param name="screen"></param>
        /// <param name="isSelected"></param>
        /// <param name="gameTime"></param>
        /// <param name="color"></param>
        /// <param name="scale"></param>
        protected void ComputeTextColorAndScale(MenuScreen screen, bool isSelected, GameTime gameTime, out Color color, out float scale)
        {
            // Draw the selected entry in yellow, otherwise white.
            color = isSelected ? Color.Yellow : Color.White;

            // Pulsate the size of the selected menu entry.
            double time = gameTime.TotalGameTime.TotalSeconds;

            float pulsate = (float)Math.Sin(time * 6) + 1;

            scale = 1 + pulsate * 0.05f * m_SelectionFade;

            // Modify the alpha to fade text out during transitions.
            color = new Color(color.R, color.G, color.B, screen.TransitionAlpha);
        }

        /// <summary>
        /// Draws the menu entry with a default scale.
        /// </summary>
        public void Draw(MenuScreen screen, Vector2 position, bool isSelected, GameTime gameTime)
        {
            Draw(screen, position, isSelected, gameTime, 1.0f);
        }

        /// <summary>
        /// Draws the menu entry. 
        /// This can be overriden, with care.
        /// </summary>
        public virtual void Draw(MenuScreen screen, Vector2 position, bool isSelected, GameTime gameTime, float fontScale)
        {
            Color color;
            float scale;
            ComputeTextColorAndScale(screen, isSelected, gameTime, out color, out scale);
            scale *= fontScale;

            // Draw text, centered on the middle of each line.
            ScreenManager screenManager = screen.ScreenManager;
            SpriteBatch spriteBatch = screenManager.SpriteBatch;
            SpriteFont font = screenManager.Font;
            Draw(spriteBatch, font, position, color, scale);
        }

        /// <summary>
        /// Actual rendering is done here.
        /// This can be overridden to customize the appearance.
        /// </summary>
        /// <param name="spriteBatch"></param>
        /// <param name="font"></param>
        /// <param name="position"></param>
        /// <param name="color"></param>
        /// <param name="scale"></param>
        protected virtual void Draw(SpriteBatch spriteBatch, SpriteFont font, Vector2 position, Color color, float scale)
        {
            Vector2 origin = new Vector2(0, font.LineSpacing / 2);
            spriteBatch.DrawString(font, m_Text, position, color, 0, origin, scale, SpriteEffects.None, 0);
        }

        /// <summary>
        /// Queries how much space this menu entry requires.
        /// </summary>
        public virtual int GetHeight(MenuScreen screen)
        {
            return (int)(screen.ScreenManager.Font.LineSpacing * 1.5f);
        }

        #endregion
    }
}
